/*
 *  lobby_window.cpp
 */

#include <unistd.h>
#include "client_app.h"
#include "client_window.h"
#include "game_info.h"
#include "game_record.h"
#include "joingame_window.h"
#include "lobby_window.h"
#include "../../common/src/debug.h"
#include "../../common/src/lock.h"
#include "../../common/src/mutex.h"

LobbyWindow::LobbyWindow(BaseObjectType* object,
		                 const Glib::RefPtr<Gtk::Builder>& builder)
                        :ClientWindow(object, builder) {
	builder->get_widget("JoinButton", btnJoinGame);
	builder->get_widget("CreateButton", btnNewGame);
	builder->get_widget("RefreshButton", btnRefresh);
	createGameTreeView(builder);
	builder->get_widget("PreviewImage", img);

	builder->get_widget_derived("GameCreateWindow", winNewGame);
    winNewGame->setLobbyWindow(this);

    builder->get_widget_derived("GameJoinWindow", winJoinGame);
    winJoinGame->setLobbyWindow(this);


	btnJoinGame->signal_clicked().connect(sigc::mem_fun(*this,
                                                     &LobbyWindow::onJoinGame));

	btnNewGame->signal_clicked().connect(sigc::mem_fun(*this,
                                                      &LobbyWindow::onNewGame));
	btnRefresh->signal_clicked().connect(sigc::mem_fun(*this,
                                                      &LobbyWindow::onRefresh));

    // Ugly hack, consecuencia de no poder pasar parametros en los constructores
    winNewGame->signal_show().connect(sigc::mem_fun(*winNewGame,
                                                    &NewGameWindow::loadData));
/*
	this->GameList->signal_cursor_changed().connect
				(sigc::mem_fun(*this, &GameListWindow::ChangePreviewImage));	
*/
}

LobbyWindow::~LobbyWindow() {}

void LobbyWindow::setApp(ClientApp* app) {
    this->app = app;
    app->addListener(this);
    winNewGame->setApp(app);
    winJoinGame->setApp(app);
}

void LobbyWindow::notifyGameInfoUpdate(ClientApp* app) {
    status = STATUS_OK;
}

void LobbyWindow::notifyGameJoinOk(ClientApp* app) {
    status = STATUS_OK;
}

void LobbyWindow::onRefresh() {
    Lock lock(&mutex);
    list_store->clear();
    app->requestGamesList();
    status = STATUS_WAITING;
    while (status == STATUS_WAITING) {
        usleep(1);
    }
    if (status == STATUS_OK) {
        Lock games_lock(app->getGames().getMutex());
        std::vector<GameInfo*>* v = app->getGames().getVector();
        for (int i = 0; i < v->size(); ++i) {
            Gtk::TreeModel::Row row = *(list_store->append());
            row[game_record.gameID] = (*v)[i]->getGameID();
            row[game_record.game_name] = (*v)[i]->getGameName();
            row[game_record.campaign_name] = (*v)[i]->getCampaignName();
            row[game_record.difficulty] = (*v)[i]->getCampaignDifficulty();
            row[game_record.players_size] = (*v)[i]->getPlayersSize();
        }
    }
}

void LobbyWindow::onJoinGame(){
    Lock lock(&mutex);
    Gtk::TreeModel::Row r = *tvGames->get_selection()->get_selected();
	if (r == NULL) return;
	std::string gameID = r->get_value(game_record.gameID).data();
    app->requestJoinGame(gameID);
    status = STATUS_WAITING;
    while (status == STATUS_WAITING) {
        usleep(1);
    }
    if (status == STATUS_OK) {
        winJoinGame->show();
        get_application()->add_window(*winJoinGame);
        get_application()->remove_window(*this);
        hide();
    }
}

void LobbyWindow::onNewGame() {
    Lock lock(&mutex);
    winNewGame->show();    
    get_application()->add_window(*winNewGame);	  
    get_application()->remove_window(*this);
    hide();
}

/*
void GameListWindow::ChangePreviewImage() {
	if (*GameList->get_selection()->get_selected() != NULL) {
		Gtk::TreeModel::Row Row = *GameList->get_selection()->get_selected();
		this->PreviewImage->set(Row->get_value(model.ImagePath).data());
	}
}

*/

void LobbyWindow::createGameTreeView(const Glib::RefPtr<Gtk::Builder>& b) {
    //build tree view
    b->get_widget("GameList", tvGames);
	list_store = Gtk::ListStore::create(game_record);
	b->get_widget("GameList", tvGames);
	tvGames->set_model(list_store);
	tvGames->append_column("GameID", game_record.gameID);
	tvGames->append_column("Game", game_record.game_name);
	tvGames->append_column("Campaign", game_record.campaign_name);
	tvGames->append_column("Difficulty", game_record.difficulty);
	tvGames->append_column("Players", game_record.players_size);
}

//{+}

